package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.constant.CommonConstant;
import net.wanho.dto.InStoreDTO;
import net.wanho.dto.PurchaseDTO;
import net.wanho.po.InStore;
import net.wanho.po.Product;
import net.wanho.service.InStoreService;
import net.wanho.service.ProductService;
import net.wanho.service.StoreService;
import net.wanho.vo.InStoreVo;
import net.wanho.vo.PageInfo;
import net.wanho.vo.R;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/instore")
public class InStroeController {
    private final StoreService storeService;
    private final InStoreService inStoreService;
    private final ProductService productService;
    @GetMapping("/store-list")
    public R storeList(){
        return R.success(storeService.list());
    }
    @GetMapping("/instore-page-list")
    public R instorePageList(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             InStoreDTO inStoreDTO){
        PageInfo<InStoreVo> pageInfo = inStoreService.inStorePageList(pageNum, pageSize, inStoreDTO);
        return R.success(pageInfo);
    }

    @GetMapping("/{id}")
    public R instoreDetail(@PathVariable Integer id){
        return R.success(inStoreService.getById(id));
    }

    @PutMapping("/instore-confirm")
    public R instoreConfirm(@RequestBody InStore inStore){
        Product product=new Product();
        product=productService.getById(inStore.getProductId());
        product.setProductInvent(product.getProductInvent()+inStore.getInNum());
        productService.updateById(product);

        inStore.setIsIn(CommonConstant.IS_IN);
        inStoreService.updateById(inStore);
        return R.success("确认入库成功");
    }

    @GetMapping("/exportTable")
    public R exportTable(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             InStoreDTO inStoreDTO){
        PageInfo<InStoreVo> pageInfo = inStoreService.inStorePageList(pageNum, pageSize, inStoreDTO);
        return R.success(pageInfo.getResultList());
    }

}
